<!DOCTYPE html>
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<meta name="viewport" content="width=device-width,initial-scale=1, minimum-scale=1, maximum-scale=1,user-scalable=no">
<meta name="format-detection" content="telephone=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<link href="/css/sign_css/index.css" rel="stylesheet" type="text/css">
<link href="/css/sign_css/lrtk.css" rel="stylesheet" type="text/css">
<link href="/css/sign_css/special_12.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="/js/plugins/jquery/jquery.min.js"></script>
<script type="text/javascript" src="/js/sign_js/lrtk.js"></script>
<script src="/js/plugins/jrender/jrender.min.js"></script>
<link rel="stylesheet" href="/js/plugins/dialog2/dialog.css">
<script src="/js/plugins/dialog2/dialog.min.js"></script>

<title>积分兑换专区</title>
    <style>
        body{
            background: #e8003a;
            padding:0; margin:0;
        }
        .clearfix{ clear:both;}
        .select_container{ width:100%; border-bottom:solid 1px #cdcdcd; background:white;}
        .select_container li{ width:50%; float:left; border-right :solid 1px #cdcdcd; line-height:30px; font-size:16px; color:#1a1a1a; margin:2% 0;}
        .select_box{ width:100%; background:#fff;}
        .select_content{ width:auto;}
        .select_content li{ width:90%; margin:0 auto; line-height:30px; color:#1a1a1a; font-size:14px;}
        .select_content li img{ float:left;}
        .select_content li label{ float:left; margin-left:10px;}
    </style>



    <script>
        $(function () {
            //获取装载数据的input框
            var currentInput = $("#currentPage");
            currentInput.val(1);
            //创建用于存储游记的数组
            var productlArr = [];
            //总页数
            var pages = 1;
            //商品价值积分
            var productSign = 0;
            var productId;
            //我的积分
            var signScore = 0;

            //获取游记数据的方法
            function query() {
                //获取当前页的值
                var currentPage = currentInput.val();
                $.get('/products', {currentPage:currentPage}, function (data) {
                    console.log(data);
                    //合并两个数组到第一个参数数组上
                    $.merge(productlArr, data.list);
                    //用于渲染时循环,循环时是取list的属性出来
                    var json = {
                        list: productlArr
                    };
                    console.log(json);

                    //渲染数据
                    $("#allProduct").renderValues(json);   /*因为页面要渲染,所以操作要紧跟渲染后*/

                    //点击兑换积分
                    $(".clickBtn").click(function () {
                      var id = $(this).closest('div').find('input').first().attr("value");
                        console.log(id);
                        //发送ajax去查商品积分
                        $.get("/products/selectProductById",{id:id},function (data) {

                        if (data.success) {
                            productSign=data.result.productSign;
                            productId = data.result.id;
                            console.log(productSign);
                            //因为商城是不登录都能看的,所以查积分之前,先判断当前用户是否登录
                            $.get("/products/userId", function (data) {
                                if (data.result !=null){
                                    console.log(data);
                                    //发送ajax去查出我的积分
                            $.get("/signs", function (data) {
                                signScore = data.result.signScore;
                                console.log(signScore);
                                //根据判断弹窗
                                if (data.success) {
                                    if (signScore >= productSign) {  //根据积分够不够判断兑换成功还是失败
                                        $(document).dialog({
                                            type: 'confirm',
                                            closeBtnShow: true,
                                            content: '你确定要兑换当前商品吗?',
                                            buttonTextConfirm: '现在立刻马上兑换!',
                                            buttonTextCancel: '突然不想兑换了',
                                            onClickConfirmBtn: function () {
                                                //马上兑换 :请求扣除积分
                                                $.get("/signs/productScore",{productScore:productSign,productId:productId})

                                                //把创建时间查回来
                                                $.get("/signs/changeTime",function (data) {
                                                    if (data.success) {
                                                        console.log("lala"+data);
                                                    }
                                                })

                                                var toast5 = $(document).dialog({
                                                    type: 'toast',
                                                    infoIcon: 'http://pgf7k79ux.bkt.clouddn.com/835746b36e3be7f4.png',
                                                    infoText: '兑换成功',
                                                    autoClose: 700,
                                                })
                                            },
                                            onClickCancelBtn: function () {
                                                var toast5 = $(document).dialog({
                                                    type: 'toast',
                                                        infoIcon: 'http://pgf7k79ux.bkt.clouddn.com/20181012165759.png',
                                                        infoText: '兑换失败',
                                                        autoClose: 700,
                                                }, 500);
                                            },
                                        });

                                    } else { //积分不够
                                        var toast5 = $(document).dialog({
                                            type: 'toast',
                                            infoIcon: 'http://pgf7k79ux.bkt.clouddn.com/e6ab85e2d5a0825fc91d3e25d3ec072b.png',
                                            infoText: '当前积分不够哦',
                                            autoClose: 700,
                                        })
                                    }
                                }
                            })
                            }else{ //如果没登录,跳转登录页
                                 window.location.href="/login.html"
                                }
                        })
                        }
                    })
                })

            //设置总页数
            pages = data.pages;

            });
            //当前页input框+1
            currentInput.val(parseInt(currentPage) + 1);
        }
        //默认查询第一页的数据
        query();
        //当页面滚动条变化时，执行的函数
        $(window).scroll(function () {
            if ($(document).scrollTop() + $(window).height() >= $(document).height()) {
                var currentPage = currentInput.val();

                //判断是否小于或等于最后一页,如果是就去查询
                if (currentPage <= pages) {
                    query();
                } else {
                    $(document).dialog({
                        type: 'notice',
                        infoText: '已经到底了!',
                        autoClose: 1000,
                        position: 'bottom'
                    });
                }
            }
        });
    })
    </script>

</head>
<body id="scoll_d" style="background-color: transparent;">
<header>
	<div id="gduo" class="top" style="height:50px;">
		<font class="title">积分兑换专区</font>
		<a href="/mine/profiles.html" class="return">
            <img src="http://pgf7k79ux.bkt.clouddn.com/gd_black.png"></a>
		<span class="gwc_img">
		    <a href="/index.html" class="ret_home"><img src="http://pgf7k79ux.bkt.clouddn.com/gg_home.png"></a>
        </span>

		</div>
</header>
<ul class="select_container">
    <li i="class"><a href="/mine/mySign.html">我的积分详情 </a></li>
    <li i="price" style="border:none; width:49.6%;"><a href="/mine/changeList.html">我的兑换列表 </a></li>
    <div class="clearfix"></div>
</ul>

<input type="hidden" name="currentPage" id="currentPage" value="1">


<div class="cp" id="allProduct">
    <!--遍历-->
    <div render-loop="list">
        <div>
    <dl class="carousel-item" style="height: 130px">
        <dt>
        <h2 style="height:38px;overflow:hidden" render-html="list.productTitle"></h2>
        <p class="asd">
            <span class="price"><font render-html="list.productSign"></font> 积分
                <html>市场价:</html>
                <del class="sale" render-html="list.productPrice"></del></span>
                <span class="clickBtn"><b>立即兑换</b></span>

                <input name="id" type="hidden" render-value="list.id">
        </p>
        </dt>
        <dd><img render-src="list.productUrl"></dd>
    </dl>
        </div>
    </div>
</div>
</div>

</body>
</html>